package com.gxuwz.lhleavesys.web.servlet;

import com.gxuwz.lhleavesys.context.AppContext;
import com.gxuwz.lhleavesys.entity.Teacher;
import com.gxuwz.lhleavesys.service.TeacherService;
import com.gxuwz.lhleavesys.service.impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/TeacherServlet")
public class TeacherServlet extends BaseLHServlet {

    private String view_teacher = "/views/admin/teacherList.jsp";// 辅导员管理首页面
    private String view_add = "/views/admin/addTeacher.jsp";// 辅导员添加页面
    private String view_edit = "/views/admin/editTeacher.jsp";// 辅导员删除页面

    @Autowired
    private TeacherService teacherService;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.teacherService = (TeacherServiceImpl) AppContext.getBean("teacherServiceImpl", TeacherServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if (action == null) {
            List<Teacher> teachers = teacherService.getAllorOne(request.getParameter("instID"),
                    request.getParameter("name"));
            request.setAttribute("teachers", teachers);
            doDispatch(request, response, view_teacher);
        } else if (action.equals("add")) {
            Teacher teacher = new Teacher();
            teacher.setInstID(request.getParameter("instID"));
            teacher.setName(request.getParameter("name"));
            teacher.setCollegeID(request.getParameter("collegeID"));
            teacher.setTelNumber(request.getParameter("telNumber"));
            teacher.setPassword(request.getParameter("password"));
            boolean flag = teacherService.addTeacher(teacher);
            if (flag) {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('新增成功！');window.location.href='/TeacherServlet';</script>";
                response.getWriter().write(jsCode);
                doDispatch(request, response, "/TeacherServlet");
            } else {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('新增失败，请稍后重试，或尝试重进系统');window.location.href='/TeacherServlet';</script>";
                response.getWriter().write(jsCode);
                doDispatch(request, response, "/TeacherServlet");
            }
        } else if (action.equals("delete")) {
            String ID = request.getParameter("instID");
            boolean flag = teacherService.deleteTeacher(ID);
            if (flag) {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('删除成功！');window.location.href='/TeacherServlet';</script>";
                response.getWriter().write(jsCode);
                doDispatch(request, response, "/TeacherServlet");
            } else {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('删除失败，请稍后重试，或尝试重进系统');window.location.href='/TeacherServlet';</script>";
                response.getWriter().write(jsCode);
                doDispatch(request, response, "/TeacherServlet");
            }
        } else if (action.equals("edit")) {
            String id = request.getParameter("id");
            Teacher teacher = new Teacher();
            teacher.setInstID(request.getParameter("instID"));
            teacher.setName(request.getParameter("name"));
            teacher.setCollegeID(request.getParameter("collegeID"));
            teacher.setTelNumber(request.getParameter("telNumber"));
            teacher.setPassword(request.getParameter("password"));
            boolean flag = teacherService.updateTeacher(teacher, id);
            if (flag) {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('修改成功');window.location.href='/TeacherServlet';</script>";
                response.getWriter().write(jsCode);
                doDispatch(request, response, "/TeacherServlet");
            } else {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('修改失败，请稍后重试，或尝试重进系统');window.location.href='/TeacherServlet';</script>";
                response.getWriter().write(jsCode);
                doDispatch(request, response, "/TeacherServlet");
            }
        } else if (action.equals("viewadd")) {
            doDispatch(request, response, view_add);
        } else if (action.equals("viewedit")) {
            String ID = request.getParameter("instID");
            Teacher teacher = teacherService.getTeacherByInstID(ID);
            request.setAttribute("teacher", teacher);
            doDispatch(request, response, view_edit);
        }
    }

    /*
     * private void addDepartment(HttpServletRequest request, HttpServletResponse
     * response) throws IOException {
     * String depID = request.getParameter("depID");
     * String depName = request.getParameter("depName");
     * 
     * Department department = new Department();
     * department.setDepID(depID);
     * department.setDepName(depName);
     * 
     * departmentService.addDepartment(department);
     * response.sendRedirect("departments.jsp");
     * }
     * 
     * private void deleteDepartment(HttpServletRequest request, HttpServletResponse
     * response) throws IOException {
     * String depID = request.getParameter("depID");
     * 
     * departmentService.deleteDepartment(depID);
     * response.sendRedirect("departments.jsp");
     * }
     * 
     *//*
        * private void updateDepartment(HttpServletRequest request, HttpServletResponse
        * response) throws IOException {
        * String depID = request.getParameter("depID");
        * String depName = request.getParameter("depName");
        * 
        * Department department = new Department();
        * department.setDepID(depID);
        * department.setDepName(depName);
        * 
        * departmentService.updateDepartment(department,depID);
        * response.sendRedirect("departments.jsp");
        * }
        *//*
           * 
           * private void viewDepartment(HttpServletRequest request, HttpServletResponse
           * response) throws IOException, ServletException {
           * String depID = request.getParameter("depID");
           * Department department = departmentService.getDepartmentByDepID(depID);
           * request.setAttribute("department", department);
           * RequestDispatcher dispatcher =
           * request.getRequestDispatcher("department_view.jsp");
           * dispatcher.forward(request, response);
           * }
           */

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }
}
